package com.alex.index.insert;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import com.alex.common.config.IndexConfig;
import com.alex.entity.ArticleReferenceEntity;
import com.alex.index.create.A_CreateIndex_Reference;
import com.alex.utils.EsClient;
import com.alex.utils.OftenUtils;
import com.alex.utils.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@SuppressWarnings("all")
@RequestMapping("/article_reference")
public class B_Article_Reference_Insert {

    public static void main(String[] args) throws Exception {
        A_CreateIndex_Reference.main(null);
        start();
    }


    @GetMapping(value = "insert")
    public void insert() throws Exception {
        start();
    }

    public static void start() throws IOException, SQLException {
        Db use = Db.use();

        String pm_ut_aid_info = IndexConfig.db_article;
        ElasticsearchClient client = EsClient.client();

        String startTimeStr = DateUtil.now();

        System.out.println("程序开始");


        /**
         * 开始准备期刊表
         */
        int from = 1;
        int max = 0;
        String autoId = "id";
        List<Entity> tableNum = use.query("select count(1) as count from " + pm_ut_aid_info + " ; ");
        System.out.println("读表长度为:" + tableNum.get(0).getInt("count"));
        int count = tableNum.get(0).getInt("count");
        if (count > 0) {
            max = use.queryNumber("select max(id) from " + pm_ut_aid_info).intValue();
        }
        Integer onePageNum = 100;
        int page = max / onePageNum;
        if (max % onePageNum != 0) {
            page++;
        }

        System.out.println("总页数___" + page);
        List<OftenUtils.PageInfo> pageInfos = new ArrayList<>();
        for (int i = 1; i <= page; i++) {
            int start = (i - 1) * onePageNum + from;
            int stop = i * onePageNum + from - 1;
            if (stop > max) {
                stop = max + onePageNum;
            }
            pageInfos.add(new OftenUtils.PageInfo(start, stop));
        }


        pageInfos.forEach(pg -> {


            List<BulkOperation> bulkOperations = new ArrayList<>();


            List<Entity> list = null;
            try {
                list = use.query("select id,aid,cite,journal,year,is_cns,jcr_if,pmid,ab,cn_ab,discussion,cn_discussion,results,cn_results,methods,cn_methods,background,cn_background from " + pm_ut_aid_info + " where pmid !=0 AND " + autoId + " between " + pg.getFrom() + " and " + pg.getTo());
            } catch (SQLException e) {
                e.printStackTrace();
            }

            for (Entity entry : list) {
                Integer aid = entry.getInt("aid");
                String cite = OftenUtils.getStrField(entry, "cite");
                String journal = toTitleCase(OftenUtils.getStrField(entry, "journal"));
                Integer year = OftenUtils.getIntField(entry, "year");
                Integer is_cns = OftenUtils.getIntField(entry, "is_cns");
                Double jcr_if = OftenUtils.getDouField(entry, "jcr_if");

                List<ArticleReferenceEntity> finalList = new ArrayList<>();
                {
                    String discussion = entry.getStr("discussion");
                    if (StringUtil.isNotNull(discussion)) {
                        if (!discussion.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(discussion);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Discussion");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Discussion");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }
                {
                    String results = entry.getStr("results");
                    if (StringUtil.isNotNull(results)) {
                        if (!results.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(results);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Results");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Results");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }
                {
                    String methods = entry.getStr("methods");
                    if (StringUtil.isNotNull(methods)) {
                        if (!methods.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(methods);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Methods");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Methods");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }
                {
                    String background = entry.getStr("background");
                    if (StringUtil.isNotNull(background)) {
                        if (!background.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(background);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Background");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Background");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }

                {
                    String cn_discussion = entry.getStr("cn_discussion");
                    if (StringUtil.isNotNull(cn_discussion)) {
                        if (!cn_discussion.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(cn_discussion);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Discussion");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Discussion");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }
                {
                    String cn_results = entry.getStr("cn_results");
                    if (StringUtil.isNotNull(cn_results)) {
                        if (!cn_results.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(cn_results);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Results");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Results");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }
                {
                    String cn_methods = entry.getStr("cn_methods");
                    if (StringUtil.isNotNull(cn_methods)) {
                        if (!cn_methods.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(cn_methods);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Methods");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Methods");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }

                                });
                            } catch (Exception e) {

                            }
                        }
                    }
                }
                {
                    String cn_background = entry.getStr("cn_background");
                    if (StringUtil.isNotNull(cn_background)) {
                        if (!cn_background.equalsIgnoreCase("[]")) {
                            try {
                                JSONArray jsonArray = JSONObject.parseArray(cn_background);
                                jsonArray.forEach(i -> {
                                    JSONObject jsonObject = JSONObject.parseObject(i.toString());
                                    String en = jsonObject.getString("en");
                                    if (en.contains(". ")) {
                                        String[] split = en.split("\\. ");
                                        for (String string : split) {
                                            ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                            articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                            articleReference.setId(IdUtil.fastUUID());
                                            articleReference.setAid(aid);
                                            articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                            articleReference.setAddr("Background");
                                            articleReference.setCite(cite);
                                            articleReference.setJour(journal);
                                            articleReference.setYear(year);
                                            articleReference.setIs_cns(is_cns);
                                            articleReference.setJcr_if(jcr_if);
                                            finalList.add(articleReference);
                                        }
                                    } else {
                                        ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                        articleReference.setArticle(en.replaceAll("ĺļ", "").trim());
                                        articleReference.setId(IdUtil.fastUUID());
                                        articleReference.setAid(aid);
                                        articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                        articleReference.setAddr("Background");
                                        articleReference.setCite(cite);
                                        articleReference.setJour(journal);
                                        articleReference.setYear(year);
                                        articleReference.setIs_cns(is_cns);
                                        articleReference.setJcr_if(jcr_if);
                                        finalList.add(articleReference);
                                    }
                                });
                            } catch (Exception e) {
                            }
                        }
                    }
                }

                {
                    String ab = entry.getStr("ab");
                    if (StringUtil.isNotNull(ab)) {
                        try {
                            if (ab.contains(". ")) {
                                String[] split = ab.split("\\. ");
                                for (String string : split) {
                                    ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                    articleReference.setId(IdUtil.fastUUID());
                                    articleReference.setAid(aid);
                                    articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                    articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                    articleReference.setAddr("Abstract");
                                    articleReference.setCite(cite);
                                    articleReference.setJour(journal);
                                    articleReference.setYear(year);
                                    articleReference.setIs_cns(is_cns);
                                    articleReference.setJcr_if(jcr_if);
                                    finalList.add(articleReference);
                                }
                            } else {
                                ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                articleReference.setId(IdUtil.fastUUID());
                                articleReference.setAid(aid);
                                articleReference.setArticle(ab.replaceAll("ĺļ", "").trim());
                                articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                articleReference.setAddr("Abstract");
                                articleReference.setCite(cite);
                                articleReference.setJour(journal);
                                articleReference.setYear(year);
                                articleReference.setIs_cns(is_cns);
                                articleReference.setJcr_if(jcr_if);
                                finalList.add(articleReference);
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                {
                    String cn_ab = entry.getStr("cn_ab");
                    if (StringUtil.isNotNull(cn_ab)) {
                        try {
                            if (cn_ab.contains("。")) {
                                String[] split = cn_ab.split("。");
                                for (String string : split) {
                                    ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                    articleReference.setId(IdUtil.fastUUID());
                                    articleReference.setAid(aid);
                                    articleReference.setArticle(string.replaceAll("ĺļ", "").trim());
                                    articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                    articleReference.setAddr("Abstract");
                                    articleReference.setCite(cite);
                                    articleReference.setJour(journal);
                                    articleReference.setYear(year);
                                    articleReference.setIs_cns(is_cns);
                                    articleReference.setJcr_if(jcr_if);
                                    finalList.add(articleReference);
                                }
                            } else {
                                ArticleReferenceEntity articleReference = new ArticleReferenceEntity();
                                articleReference.setId(IdUtil.fastUUID());
                                articleReference.setAid(aid);
                                articleReference.setArticle(cn_ab.replaceAll("ĺļ", "").trim());
                                articleReference.setPmid(OftenUtils.getIntField(entry, "pmId"));
                                articleReference.setAddr("Abstract");
                                articleReference.setCite(cite);
                                articleReference.setJour(journal);
                                articleReference.setYear(year);
                                articleReference.setIs_cns(is_cns);
                                articleReference.setJcr_if(jcr_if);
                                finalList.add(articleReference);
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                //这里可能好几千条
                for (ArticleReferenceEntity referenceEntity : finalList) {
                    bulkOperations.add(new BulkOperation.Builder().create(d -> d.document(referenceEntity).id("" + (referenceEntity.getId()))).build());
                }
            }//FOR 循环结束 以上一个bulkOperations是一千条
            BulkResponse result = null;
            try {
                result = client.bulk(e -> e.index(IndexConfig.REFERENCE_INDEX).operations(bulkOperations));
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("开始id___" + pg.getFrom() + "结束id___" + pg.getTo());

            bulkOperations.clear();
        });
        System.out.println("完成");
    }

    /**
     * 字符串根据特定符号转list
     *
     * @param string
     * @param mark
     * @return
     */
    public static List<String> strToList(String string, String mark) {
        try {
            return Arrays.stream(string.split(mark)).collect(Collectors.toList());
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public static List<String> strToAbsList(String string, String la) {
        List<String> list = new ArrayList<>();
        try {
            JSONArray jsonArray = JSONObject.parseArray(string);
            for (Object o : jsonArray) {
                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                Object o1 = jsonObject.get(la);
                list.add(o1.toString());

            }
        } catch (Exception e) {
        }
        return list;
    }


    public static String toTitleCase(String input) {
        StringBuilder titleCase = new StringBuilder();
        boolean nextTitleCase = true;
        for (char c : input.toCharArray()) {
            if (Character.isSpaceChar(c)) {
                nextTitleCase = true;
            } else if (nextTitleCase) {
                c = Character.toTitleCase(c);
                nextTitleCase = false;
            } else {
                c = Character.toLowerCase(c);
            }
            titleCase.append(c);
        }
        return titleCase.toString();
    }

}
